<template>
	<el-select v-model="selected" v-bind="$attrs" style="width: 100%">
		<el-option v-for="(item, index) in dictEnumData" :key="index" :label="item.label" :value="item.value" />
	</el-select>
</template>

<script>
import { mapGetters } from 'vuex';
import emitter from 'element-ui/src/mixins/emitter';
export default {
	mixins: [emitter],
	name: 'swDictSelect',
	inject: { elForm: { default: () => null } },
	props: {
		value: { type: [String, Number, Array] },
		dictName: { type: String, default: '' },
		props: {
			type: Object,
			default() {
				return { label: 'msg', value: 'value' };
			}
		}
	},
	data() {
		return {
			selected: this.value
		};
	},
	computed: {
		...mapGetters(['dictEnums']),
		dictEnumData() {
			let dataArr = this.dictEnums[this.dictName] || [];
			return dataArr.map((item) => {
				return { label: item[this.props.label], value: item[this.props.value] };
			});
		}
	},
	watch: {
		value(value) {
			this.selected = value;
		},
		selected(value) {
			this.$emit('input', value);
			this.dispatch('ElFormItem', 'el.form.change', [value]);
		}
	}
};
</script>

<style scoped></style>
